<template>
	<div class="login">
		<el-form :model="fromData" :rules="rules" ref="formName" hide-required-asterisk>
			<el-form-item>
				<h2>胖虎到家-后台登录</h2>
			</el-form-item>

			<el-form-item label="账号" prop="username">
				<el-input placeholder="请输入账号" prefix-icon="el-icon-user" v-model="fromData.username">
				</el-input>
			</el-form-item>

			<el-form-item label="密码" prop="password">
				<el-input type="password" placeholder="请输入密码" prefix-icon="el-icon-lock" v-model="fromData.password">
				</el-input>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" @click="submitForm('formName')">登录</el-button>
				<el-button @click="resetForm('formName')">取消</el-button>
			</el-form-item>
		</el-form>
	</div>
</template>

<script>
	export default {
		components: {},
		data() {
			return {
				fromData: {
					username: 'admin',
					password: 'admin'
				},
				rules: {
					username: [{
							required: true,
							message: '账号不能为空',
							trigger: 'blur'
						},
						{
							min: 5,
							max: 20,
							message: '长度在5到20个字符！',
							trigger: 'blur'
						}
					],
					password: [{
							required: true,
							message: '密码不能为空',
							trigger: 'blur'
						},
						{
							min: 5,
							message: '长度至少5个字符！',
							trigger: 'blur'
						}
					],
				}
			}
		},
		methods: {
			// 登录判断
			submitForm(formName) {
				this.$refs[formName].validate((valid) => {
					if (valid) {
						this.$http.post('http://43.143.190.87:8182/login?username=' + this.fromData.username +
								'&pwd=' + this.fromData.password)
							.then((res) => {
								if (res.data.code == 1) {
									this.$message.success('登录成功')
									this.$router.push('/home')
								} else {
									this.$message.error('没有此用户')
								}
							})
							.catch((error) => {
								this.$message.error('服务器失联' + error)
							})
					} else {
						this.$message.error('账号或密码有误')
						return false;
					}
				});
			},
			// 清空数据
			resetForm(formName) {
				this.$refs[formName].resetFields();
			},
			login_success() {
				this.can = '/Quarters'
			}
		}
	}
</script>

<style scoped="scoped">
	.login {
		height: 100vh;
		background: url('../assets/backimg/login_bg.jpg') no-repeat center;
		background-size: 100% 100%;
		overflow: hidden;
	}

	.el-form {
		width: 480px;
		height: 60%;
		margin: 50px auto;
		padding-left: 50px;
		padding-top: 10px;
		background-color: whitesmoke;
		border-radius: 10px;
		box-shadow: 0 8px 16px rgba(0, 0, 0, .12), 0 0 6px rgba(0, 0, 0, .04);
	}

	.el-form-item__content {
		padding-left: 20px;
	}

	.el-form-item {
		margin: 50px auto;
	}

	.el-form-item h2 {
		text-align: center;
	}

	.el-input {
		width: 70%;
	}

	.el-button {
		margin-left: 10px;
	}
</style>